package message;

import java.io.Serializable;
import java.io.*;

public class UdpMessage implements Serializable {
	private static final long serialVersionUID = 5043496477334035389L;
	private String type;
	private String command;
	private int value;
        private String sep = "##__##";


	public UdpMessage( String command, String type, int value){
		super();
		this.type = type;
		this.value = value;
                this.command = command;
	}

	public String getType() {
		return type;
	}

	public int getValue() {
		return value;
	}

	public String getCommand() {
		return command;
	}

   public void objDeSerialize (byte[] byteArray)
   {
       try
       {
            ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
            ObjectInputStream in = new ObjectInputStream(bis);
            UdpMessage udp = (UdpMessage)in.readObject();
            this.type = udp.getType();
            this.command = udp.getCommand();
            this.value = udp.getValue();
            return;
       }
       catch(Exception x)
       {
           return;
       }
   }

   public byte[] objSerialize()
   {
       try
       {
           ByteArrayOutputStream bos = new ByteArrayOutputStream();
           ObjectOutputStream out = new ObjectOutputStream(bos);
           out.writeObject(this);
           out.close();
           return bos.toByteArray();
       }
       catch(Exception x)
       {
           return null;
       }

   }

   public byte[] ASCIISerialize()
   {
       String tmp = type + sep + command + sep + value + sep;
       return tmp.getBytes();
   }

   public void ASCIIDeSerialize(byte[] ba, int len)
   {
       String tmp = new String(ba,0,len);
       String[] sa = tmp.split(sep);
       type = sa[0];
       command = sa[1];
       value = Integer.parseInt(sa[2]);
       return;
   }

}
